<?php

namespace Modules\Coupon\Http\Controllers\Client\V1;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use Modules\Coupon\Entities\Coupon;

use Validator;
use ThrowException;
use Auth;

class CouponPickController extends Controller
{
    public function store(Request $req, $uuid)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest();

        $user = Auth::requireLoginUser();

        $item = Coupon::getEntity([
            'uuid' => $uuid
        ]);

        $item->pushTo($user);

        return [];
    }

    public function storeWithCode(Request $req)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest();

        $user = Auth::requireLoginUser();

        $code = $req->input('code');

        $item = Coupon::getEntity([
            'code' => $code
        ], false) ?: ThrowException::Conflict('无效的兑换码');

        $item->pushTo($user);

        return [];
    }
}
